import java.util.HashMap;

public class Test {
}

//leetcode:LCR008:长度最小的子数组
//滑动窗口
class Solution {
    public int minSubArrayLen(int target, int[] nums) {
        int left = 0,right = 0,sum = 0, len = Integer.MAX_VALUE;
        int n = nums.length;
        while(right < n){
            sum+=nums[right];
            while(sum >= target){
                len = Math.min(len,right-left+1);
                sum-= nums[left++];
            }
            right++;
        }
        return len == Integer.MAX_VALUE ? 0:len;
    }
}

//leetcode:LCR016:无重复字符的最长字串
//滑动窗口+哈希表
class Solution2 {
    public int lengthOfLongestSubstring(String s) {
        int ret = 0;
        HashMap<Character,Integer> map = new HashMap<>();
        int left = 0;
        for(int right = 0;right< s.length();right++){
            char ch = s.charAt(right);
            while(map.containsKey(ch)){
                char remove = s.charAt(left);
                map.remove(remove);
                left++;
            }
            map.put(ch,1);
            ret = Math.max(ret,map.size());
        }
        return ret;
    }
}
//滑动窗口+数组模拟实现哈希表
class Solution3 {
    public int lengthOfLongestSubstring(String ss) {
        char[] s = ss.toCharArray();//将字符串转换成字符数组
        int [] hash = new int[128];
        int left = 0,right = 0,n = ss.length();
        int ret = 0;
        while(right < n){
            hash[s[right]]++;
            while(hash[s[right]] > 1){
                hash[s[left++]]--;
            }
            ret = Math.max(ret,right-left+1);
            right++;
        }
        return ret;
    }
}